function [vol info] = GetCTDicomImage(Dir)
% Load stack of DICOM images and return the 3D volume and information
% extracted from the first image.
%
% Usage: 
%    >> [img ~] = GetCTDicomImage('./Cache/1008.2.18.7.1.1.2');
%    >> imagesc3(img);  % View the 3D image
%
%  - Michael Eager (michael.eager@monash.edu)

%     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
%
%     This file is part of Xglom.
% 
%     This is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.


hProgress= progressbar([],0,'Please wait while CT DICOM is being loaded.');
[status Files] = system(['ls -1 ' Dir]);
Files =regexp(Files, '\n', 'split');         % Split the output into a list
Files(cellfun(@isempty, Files))=[];          % Clean the list
info = dicominfo(fullfile(Dir, Files{1}));

vol = zeros(info.Width,info.Height,numel(Files),'single');
for ii = 1:numel(Files)
    info = dicominfo(fullfile(Dir,Files{ii}));
    id(ii) = (info.AcquisitionNumber);   
    vol(:,:,(info.AcquisitionNumber)+1) = single(dicomread(info));
    hProgress = progressbar( hProgress, 1.0/double(numel(Files)),...
        'Please wait while CT DICOM is being loaded.' );
end
progressbar( hProgress, -1 );
if ~hProgress
    disp('Progressbar removed.');
end
